﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace WindowsFormsApp2
{
    public class SQL
    {
        public SQL()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        private static SqlConnection connection;
        public static SqlConnection Connection
        {
            get
            {

                string connectString = System.Configuration.ConfigurationSettings.AppSettings["connectionstring"];
                if (connection == null || connection.State == ConnectionState.Closed)
                {
                    connection = new SqlConnection(connectString);
                    connection.Open();
                }
                return connection;
            }
        }
        public static DataTable GetDataSet(string sql)
        {
            SqlCommand command = new SqlCommand(sql, Connection);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(ds);
            return ds.Tables[0];
        }
        public static DataTable GetDataSet(string sql, SqlParameter[] sqlParameter)
        {
            SqlCommand command = new SqlCommand(sql, Connection);
            foreach (SqlParameter parameter in sqlParameter)
            {
                command.Parameters.Add(parameter);
            }
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(ds);
            return ds.Tables[0];
        }
        public static int ExecuteSql(string sql)
        {
            SqlCommand command = new SqlCommand(sql, Connection);


            //return command.ExecuteNonQuery();
            return command.ExecuteNonQuery();
        }
        public static int ExecuteSql(string sql, SqlParameter[] sqlParameter)
        {
            SqlCommand command = new SqlCommand(sql, Connection);
            foreach (SqlParameter parameter in sqlParameter)
            {
                command.Parameters.Add(parameter);
            }
            return command.ExecuteNonQuery();
        }
    }

}

